package com.yang.utils;

import com.yang.core.HttpSample;
import com.yang.core.TestResult;
import com.yang.core.TestResultsSet;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

public class ReadJTLFile {
    private ArrayList<HttpSample> httpSamples = new ArrayList<HttpSample>();

    public ArrayList<HttpSample> parseJtlFileFromXML(String XMLFilePath) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(XMLFilePath);
            NodeList nodes = doc.getElementsByTagName("httpSample");
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                NamedNodeMap attributes = node.getAttributes();
                String t = attributes.getNamedItem("t").getNodeValue();
                String ts = attributes.getNamedItem("ts").getNodeValue();
                String s = attributes.getNamedItem("s").getNodeValue();
                String lb = attributes.getNamedItem("lb").getNodeValue();
                String by = attributes.getNamedItem("by").getNodeValue();
                String sby = attributes.getNamedItem("sby").getNodeValue();
                httpSamples.add(new HttpSample(Long.parseLong(ts), Integer.parseInt(t), lb, s, Integer.parseInt(by), Integer.parseInt(sby)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return httpSamples;
    }

    public ArrayList<HttpSample> parseJtlFileFromCSV(String CSVFilePath) {
        try {
            BufferedReader in = new BufferedReader(new FileReader(CSVFilePath));
            String line = null;
            in.readLine();  //读一行，跳过标题。
            while ((line = in.readLine()) != null) {
                String[] split = line.split(",");
                // 如果JTL文件较大，可能会出现OOM，待优化。
                httpSamples.add(new HttpSample(Long.parseLong(split[0]), Integer.parseInt(split[1]), split[2], split[7], Integer.parseInt(split[9]), Integer.parseInt(split[10])));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this.httpSamples;
    }

    public TestResultsSet getResultsSet(ArrayList<HttpSample> httpSamples) {

        TestResultsSet testResultsSet = new TestResultsSet();
        TestResult total = new TestResult();//The total of aggregated report

        for (HttpSample httpSample : httpSamples) {
            testResultsSet.addTestResults(httpSample.getLabel(), httpSample);
            total.update(httpSample);
        }

        testResultsSet.getTestResults().put("total", total);
        return testResultsSet;
    }

    public static void main(String[] args) {
        String fileName = "C:\\Users\\Administrator\\Desktop\\111.xml";
        ReadJTLFile readJTLFile = new ReadJTLFile();
        ArrayList<HttpSample> httpSamples = readJTLFile.parseJtlFileFromXML(fileName);
        System.out.println(httpSamples.toString());
    }
}
